package com.chuangjiangx.member.score.ddd.query;

import com.chuangjiangx.commons.CJBeanUtils;
import com.chuangjiangx.commons.page.PagingResult;
import com.chuangjiangx.domain.shared.model.Enable;
import com.chuangjiangx.member.basic.ddd.dal.dto.MemberDetail;
import com.chuangjiangx.member.score.dao.mapper.InMbrScoreGiftRuleMapper;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGiftRule;
import com.chuangjiangx.member.score.dao.model.InMbrScoreGiftRuleExample;
import com.chuangjiangx.member.coupon.ddd.dal.mapper.MbrCouponDalMapper;
import com.chuangjiangx.member.basic.ddd.dal.mapper.MemberDalMapper;
import com.chuangjiangx.member.score.ddd.dal.dto.*;
import com.chuangjiangx.member.score.ddd.dal.mapper.ScoreGiftDalMapper;
import com.chuangjiangx.member.score.ddd.dal.condition.QueryScoreGiftRuleCondition;
import com.chuangjiangx.member.basic.ddd.query.condition.SearchMemberInfoCondition;
import com.chuangjiangx.member.score.ddd.query.dto.GiftsListAndMemberInfoDTO;
import com.chuangjiangx.member.score.ddd.query.dto.GiftsListDTO;
import com.chuangjiangx.member.score.ddd.query.dto.GiftsListForH5DTO;
import com.chuangjiangx.member.score.ddd.query.dto.NewGiftsListAndMemberInfoDTO;
import com.chuangjiangx.member.stored.ddd.dal.dto.RuleName;
import com.chuangjiangx.member.stored.ddd.query.dto.RuleNameDTO;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Collections;
import java.util.List;

/**
 * 兑换规则查询
 */
@Component
public class MbrScoreGiftRuleQuery {

    @Autowired
    private  ScoreGiftDalMapper scoreGiftDalMapper;
    @Autowired
    private  MemberDalMapper memberDalMapper;
    @Autowired
    private InMbrScoreGiftRuleMapper inMbrScoreGiftRuleMapper;
    @Autowired
    private MbrCouponDalMapper mbrCouponDalMapper;

    /**
     * 查询所有的规则
     */
    public List<RuleNameDTO> queryAllRuleName() {
        List<RuleName> list = scoreGiftDalMapper.queryAllRuleName();
        List<RuleNameDTO> dtoList = CJBeanUtils.convertCollection(list, RuleNameDTO.class);
        return dtoList;
    }


    /**
     * 积分商城列表查询-客户端
     *
     * @param condition 所属商户id 会员手机号
     * @return
     */
    public GiftsListAndMemberInfoDTO queryGiftsList(SearchMemberInfoCondition condition) throws Exception {

        GiftsListAndMemberInfoDTO giftsListAndMemberInfoDTO = new GiftsListAndMemberInfoDTO();

        //1、查询商品列表
        List<GiftsListDTO> giftsListDTOs = scoreGiftDalMapper.queryGiftsList(condition.getMerchantId());
        MemberDetail memberDetail;

        //2、查询会员信息,1判断手机号码是否为空,2判断手机号码是否为会员
        if (StringUtils.isBlank(condition.getMobile())) {
            memberDetail = null;
        } else {
            memberDetail = memberDalMapper.searchMemberDetailByMobile(condition);
        }

        // 3、信息整合
        if (memberDetail == null) {
            //0：代表积分不足 ，1代表可兑换
            giftsListDTOs.forEach(giftsListDTO -> giftsListDTO.setAvailableConvert((long) 1));
        } else {
            giftsListDTOs.forEach(giftsListDTO -> {

                //用于比较商品兑换积分与用户可用积分大小，结果是:-1 小于,0 等于,1 大于
                int i = memberDetail.getAvailableScore().compareTo(giftsListDTO.getScore());
                if (-1 == i) {

                    //0：代表积分不足 ，1代表可兑换
                    giftsListDTO.setAvailableConvert((long) 0);
                } else {
                    giftsListDTO.setAvailableConvert((long) 1);
                }
            });
        }

        giftsListAndMemberInfoDTO.setGiftsListDTOs(giftsListDTOs);
        giftsListAndMemberInfoDTO.setMemberDetail(memberDetail);

        return giftsListAndMemberInfoDTO;
    }


    /**
     * 积分商城列表查询-客户端(新版)
     *
     * @param condition 所属商户id 会员手机号
     * @return
     */
    public NewGiftsListAndMemberInfoDTO queryGiftsListForClient(SearchMemberInfoCondition condition) throws Exception {

        NewGiftsListAndMemberInfoDTO giftsListAndMemberInfoDTO = new NewGiftsListAndMemberInfoDTO();

        //1、查询商品列表
        List<GiftsListForH5DTO> giftsListForH5DTOs = scoreGiftDalMapper.queryGiftsListForH5(condition.getMerchantId());
        MemberDetail memberDetail;

        //2、查询会员信息,1判断手机号码是否为空,2判断手机号码是否为会员
        if (StringUtils.isBlank(condition.getMobile())) {
            memberDetail = null;
        } else {
            memberDetail = memberDalMapper.searchMemberDetailByMobile(condition);
        }

        // 3、信息整合
        if (memberDetail == null) {
            giftsListForH5DTOs.forEach(giftsListForH5DTO -> {
                //商品兑换次数
                int convertCount = scoreGiftDalMapper.searchConvertCount(giftsListForH5DTO.getId());

                Long count = giftsListForH5DTO.getCount();

                if (count == null) {
                    count = 0l;
                }

                if (convertCount < count) {
                    //0：代表积分不足 ，1代表可兑换，2代表已经兑完
                    giftsListForH5DTO.setAvailableConvert((long) 1);
                } else {
                    giftsListForH5DTO.setAvailableConvert((long) 2);
                }
                giftsListForH5DTO.setOverConvertCount((long) convertCount);
            });
        } else {
            giftsListForH5DTOs.forEach(giftsListForH5DTO -> {
                //商品兑换次数
                int convertCount = scoreGiftDalMapper.searchConvertCount(giftsListForH5DTO.getId());

                Long count = giftsListForH5DTO.getCount();

                if (count == null) {
                    count = 0l;
                }

                if (convertCount < count) {//比较商品可兑换次数与已经兑换次数大小
                    //用于比较商品兑换积分与用户可用积分大小，结果是:-1 小于,0 等于,1 大于
                    int i = memberDetail.getAvailableScore().compareTo(giftsListForH5DTO.getScore());
                    if (-1 == i) {
                        giftsListForH5DTO.setAvailableConvert((long) 0);
                    } else {
                        giftsListForH5DTO.setAvailableConvert((long) 1);
                    }
                } else {
                    giftsListForH5DTO.setAvailableConvert((long) 2);
                }
                giftsListForH5DTO.setOverConvertCount((long) convertCount);
            });
        }
        giftsListAndMemberInfoDTO.setGiftsListDTOs(giftsListForH5DTOs);
        giftsListAndMemberInfoDTO.setMemberDetail(memberDetail);
        return giftsListAndMemberInfoDTO;
    }


    /**
     * 查询商品列表 -H5
     *
     * @param condition 会员手机号码
     * @return
     * @throws Exception
     */
    public List<GiftsListForH5DTO> searchGoodsListForH5(SearchMemberInfoCondition condition) throws Exception {

        //查询会员信息
        MemberDetail memberDetail = memberDalMapper.searchMemberDetailByMobile(condition);

        //查询商品列表
        List<GiftsListForH5DTO> giftsListForH5DTOs = scoreGiftDalMapper.queryGiftsListForH5(memberDetail.getMerchantId());

        giftsListForH5DTOs.forEach(giftsListForH5DTO -> {

            //商品兑换次数
            int convertCount = scoreGiftDalMapper.searchConvertCount(giftsListForH5DTO.getId());

            Long count = giftsListForH5DTO.getCount();

            if (count == null) {
                count = 0l;
            }

            if (convertCount < count) {//比较商品可兑换次数与已经兑换次数大小

                //用于比较商品兑换积分与用户可用积分大小，结果是:-1 小于,0 等于,1 大于
                int i = memberDetail.getAvailableScore().compareTo(giftsListForH5DTO.getScore());
                if (-1 == i) {
                    //0：代表积分不足 ，1代表可兑换，2代表已经兑完
                    giftsListForH5DTO.setAvailableConvert((long) 0);
                } else {
                    giftsListForH5DTO.setAvailableConvert((long) 1);
                }
            } else {
                giftsListForH5DTO.setAvailableConvert((long) 2);
            }
            giftsListForH5DTO.setOverConvertCount((long) convertCount);
        });
        return giftsListForH5DTOs;
    }

    /**
     * 通用于H5 \ APP  | WEB
     */
    public PagingResult<MemberScoreGiftRuleList> findMbrScoreGiftRuleList(QueryScoreGiftRuleCondition condition) {
        int total = scoreGiftDalMapper.countMbrScoreGiftRuleList(condition);
        List<MemberScoreGiftRuleList> list = Collections.EMPTY_LIST;
        if (total > 0) {
            list = scoreGiftDalMapper.findMbrScoreGiftRuleList(condition);
        }
        return new PagingResult<>(total,list);
    }

    public MbrScoreGiftRuleDetail findByIdAndMerchantId(Long id, Long merchantId){
        InMbrScoreGiftRuleExample example = new InMbrScoreGiftRuleExample();
        example.createCriteria()
                .andIdEqualTo(id)
                .andMerchantIdEqualTo(merchantId);
        List<InMbrScoreGiftRule> list = inMbrScoreGiftRuleMapper.selectByExample(example);
        if(list.isEmpty()){
            return null;
        }
        MbrScoreGiftRuleDetail detail = new MbrScoreGiftRuleDetail();
        BeanUtils.copyProperties(list.get(0),detail);
        detail.setStoreLists(mbrCouponDalMapper.findStoreListByMerchantId(detail.getMerchantId(),null));
        return detail;
    }

    public List<InMbrScoreGiftRule> findValidList(Long merchantId){
        InMbrScoreGiftRuleExample example = new InMbrScoreGiftRuleExample();
        example.createCriteria()
                .andMerchantIdEqualTo(merchantId)
                .andEnableEqualTo(Enable.ENABLE.value);
        List<InMbrScoreGiftRule> list = inMbrScoreGiftRuleMapper.selectByExample(example);
        return list;
    }
}
